package com.efast.cafe.commponent.query.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.efast.cafe.framework.bean.QueryPageBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.cafe.commponent.query.service.IQueryService;
import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.controller.AbstractController;
import com.efast.cafe.framework.spring.SpringContextUtils;
import com.efast.cafe.portal.bean.query.QueryBean;
import com.efast.cafe.portal.bean.query.QueryConditionBean;
import com.efast.cafe.portal.remote.query.QueryTemplateService;
import com.efast.cafe.util.StringUtil;

import java.util.ArrayList;


@Controller
@RequestMapping("/frame_center/define")
public class QueryController extends AbstractController{

	@Autowired
	private QueryTemplateService queryTemplateService;


	/**
	 * 通用查询
	 * @param request
	 * @param response
	 * @param queryConditionBean
	 * @param code
	 * @param isForTemplate
	 * @return
	 */
	@RequestMapping(value = "/query/list/{code}/{isForTemplate}")
	public @ResponseBody QueryBean list(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestBody(required = false) QueryConditionBean queryConditionBean,@PathVariable("code") String code,@PathVariable("isForTemplate") String isForTemplate){
		IQueryService queryService = getQueryService(code);
		QueryBean querys = new QueryBean();
		querys.setQueryConditionBean(queryConditionBean);
		//只需要取url的code就可以
		//template和queryConditionBean的数据都放在QueryPageHepler类进行处理
		if (!StringUtil.isEmpty(code)) {
			if (queryConditionBean == null) {
				queryConditionBean = new QueryConditionBean();
			}
			querys = queryTemplateService.queryTempalteDetailByCode(code,queryConditionBean.getTemplateSaveId());

		} else {
			querys = queryTemplateService.queryTempalteDetailByCode(queryConditionBean.getCode(),queryConditionBean.getTemplateSaveId());
		}
		querys.setQueryConditionBean(queryConditionBean);
		if ("template".equals(isForTemplate)) {
			QueryPageBase queryPageBase = new QueryPageBase();
			querys.setObject(queryPageBase);
			return querys;
		}
		Object res = queryService.queryList(querys);
		querys.setObject(res);
		return querys;
	}

	/**
	 * 通用查询
	 * @param request
	 * @param response
	 * @param queryConditionBean
	 * @param code
	 * @return
	 */
	@RequestMapping(value = "/query/list/{code}")
	public @ResponseBody QueryBean list(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestBody(required = false) QueryConditionBean queryConditionBean,@PathVariable("code") String code){
		IQueryService queryService = getQueryService(code);
		QueryBean querys = new QueryBean();
		querys.setQueryConditionBean(queryConditionBean);
		//只需要取url的code就可以
		//template和queryConditionBean的数据都放在QueryPageHepler类进行处理
		if (!StringUtil.isEmpty(code)) {
			if (queryConditionBean == null) {
				queryConditionBean = new QueryConditionBean();
			}
			querys = queryTemplateService.queryTempalteDetailByCode(code,queryConditionBean.getTemplateSaveId());

		} else {
			querys = queryTemplateService.queryTempalteDetailByCode(queryConditionBean.getCode(),queryConditionBean.getTemplateSaveId());
		}
		querys.setQueryConditionBean(queryConditionBean);
		Object res = queryService.queryList(querys);
		querys.setObject(res);
		return querys;
	}

	/**
	 * list数据类型加载下拉框数据
	 * @param request
	 * @param response
	 * @param queryConditionBean
	 * @return
	 */
	@RequestMapping(value = "/query/optionList")
	public @ResponseBody ResultBean optionList(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestBody QueryConditionBean queryConditionBean){
		QueryBean querys = new QueryBean();
		querys.setQueryConditionBean(queryConditionBean);
		if (!StringUtil.isEmpty(queryConditionBean.getCode())) {
			IQueryService queryService = getQueryService(queryConditionBean.getCode());
			querys = queryTemplateService.queryTempalteDetailByCode(queryConditionBean.getCode(),queryConditionBean.getTemplateSaveId());
			querys.setQueryConditionBean(queryConditionBean);
			Object res =  queryService.queryList(querys);
			querys.setObject(res);
		}
		return convertQueryBean(querys);
	}

	@RequestMapping(value = "/query/export/{code}")
	public @ResponseBody QueryBean export(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestBody QueryConditionBean queryConditionBean,@PathVariable("code") String code){
		IQueryService queryService = getQueryService(code);
		QueryBean querys = new QueryBean();
		querys.setQueryConditionBean(queryConditionBean);
		if (!StringUtil.isEmpty(code)) {
			querys = queryTemplateService.queryTempalteDetailByCode(code,queryConditionBean.getTemplateSaveId());
		}
		querys.setQueryConditionBean(queryConditionBean);
		querys.getTemplate().setPagenation(0);
		querys.getTemplate().setMaxRowsInNoPage(null);
		Object res = queryService.queryList(querys);
		querys.setObject(res);
		return querys;
	}
	public ResultBean convertQueryBean(QueryBean querys) {
		ResultBean bean = new ResultBean();
		if (querys == null) {
			return bean;
		}
		bean.setObject(querys.getObject());
		bean.setSuccess(querys.isSuccess());
		bean.setMessage(querys.getMessage());
		return bean;
	}

	private IQueryService getQueryService(String templateCode){
		if(SpringContextUtils.containsBean(templateCode)){
			return (IQueryService)SpringContextUtils.getBean(templateCode);
		}else{
			return (IQueryService)SpringContextUtils.getBean("defaultQueryService");
		}
	}
}
